package com.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import struts.annotation.Controller;
import struts.annotation.RequestMapping;
import struts.annotation.ResponseBody;

import com.bean.Data;
import com.bean.Student;

@Controller
@RequestMapping("/user")
public class LoginController {
	@RequestMapping("/index")
	public String login(HttpServletRequest request) {
		Map<Integer, Student> map = Data.getDbMap();
		List<Student> students = new ArrayList<Student>();
		for (Integer key : map.keySet()) {
			students.add(map.get(key));
		}
		request.setAttribute("students", students);
		return "/index.jsp";

	}

	@RequestMapping("/find")
	@ResponseBody
	public Student find(Student student) {
		return Data.getDbMap().get(student.getId());
	}

	@RequestMapping("/add")
	public String add(Student student) {

		Random random = new Random(new Date().getTime());
		Integer id = random.nextInt(9999);
		student.setId(id);
		Data.getDbMap().put(id, student);

		return "forward:/user/index";
	}

}
